<template>
  <div id="globalHead">
    <a-row :wrap="false">
      <a-col flex="200px">
        <div id="title-bar">
          <router-link to="/">
            <div class="logo">
              <img src="../assets/logo.png" alt="logo" />
            </div>
          </router-link>
          <div class="title">
            <div class="title-text">葵智能图库</div>
          </div>
        </div>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuChange"
        />
      </a-col>
      <a-col flex="120px">
        <div class="button">
          <div v-if="currentUser.loginUser.id">
            <a-dropdown>
              <a-space>
                <a-avatar :src="currentUser.loginUser.userAvatar" />
                {{ currentUser.loginUser.userName ?? '无名' }}
              </a-space>
              <template #overlay>
                <a-menu>
                  <a-menu-item @click="doLoginOut">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { AppstoreOutlined, HomeTwoTone, LogoutOutlined } from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { userLoginUserInfo } from '@/stores/userLoginUserInfo.ts'
import { loginOutUsingPost } from '@/api/yonghujiekou.ts'

const currentUser = userLoginUserInfo()
currentUser.fetchLoginUser()
// 原始菜单
const originItems = [
  {
    key: '/',
    icon: () => h(HomeTwoTone),
    label: '首页',
    title: '首页',
  },
  {
    key: '/admin/manager',
    icon: () => h(AppstoreOutlined),
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/add_picture',
    icon: () => h(AppstoreOutlined),
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: '/admin/picture_manager',
    icon: () => h(AppstoreOutlined),
    label: '图片管理',
    title: '图片管理',
  },
]
// 鉴权后的菜单
const filterMenu = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    if (menu?.key?.startsWith('/admin')) {
      // 只有管理员才能访问
      const loginUser = currentUser.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}
const items = computed(() => {
  return filterMenu(originItems)
})
const router = useRouter()
// 设计菜单选中项
const current = ref<string[]>([])
// 使用路由钩子设置菜单选中项，避免刷新页面后菜单选中项丢失
router.afterEach((to, from, failure) => {
  current.value = [to.path]
})
// 设计菜单点击事件进行页面跳转
const doMenuChange = ({ key }: any) => {
  router.push({
    path: key,
  })
}
const doLoginOut = async () => {
  const res = await loginOutUsingPost()
  if (res.data.code === 0) {
    currentUser.setLoginUser({
      userName: '未登录',
    })
    router.push({
      path: '/user/login',
      replace: true,
    })
    message.success('退出登录成功')
  }
}
</script>
<style scoped>
#title-bar {
  display: flex;
}

.logo img {
  border-radius: 10%;
  width: 50px;
  height: 48px;
}

#title-bar .title .title-text {
  margin-left: 5px;
  font-size: 20px;
}

.button .login-button {
  margin-left: 25px;
}
</style>
